﻿Imports System
Imports System.IO
Imports System.Configuration
Imports System.Data.SqlClient

Public Class DataMonitor

    ''' <summary>
    ''' Retrieves view names for views that have accumulated data since the last sync operation and are currently enabled.
    ''' </summary>
    ''' <returns>ArrayList</returns>
    ''' <remarks>Shared function that relies on a sql stored procedure to check for new records.</remarks>
    Public Shared Function ListNewData() As ArrayList
        Dim arylstTables As New ArrayList

        Try
            'First, I need to get a connection to the database and run a stored procedure
            Using conn As New SqlConnection(SetConnectionString())
                'Set and execute our command
                Using cmd As New SqlCommand("EXEC dbo.usp_GetPushRecordCount", conn)
                    'Open the connection
                    conn.Open()

                    'TODO: I had to add this to keep my archaic laptop from timing out, but even after adding, it still times out occasionally.  I probably need to look into throttling!
                    cmd.CommandTimeout = My.Settings.DataCheckInterval

                    Dim reader As SqlDataReader = Nothing 'SqlDataReader to read our stored procedure results

                    reader = cmd.ExecuteReader

                    'Read through our results and add them to the return array
                    While reader.Read()
                        If reader(1) > 0 Then
                            'Now add this file to the arraylist
                            arylstTables.Add(reader(0).ToString)
                        End If
                    End While
                End Using
            End Using
        Catch sqlex As SqlException
            'TODO: handle some of the common database exceptions here (e.g. can't connect to database, invalid stored procedure found, security exception, etc)
            System.Diagnostics.EventLog.WriteEntry("VOEIS Hub Push Service", "SQL Exception in the DataMonitor class." & Environment.NewLine & sqlex.Message.ToString, EventLogEntryType.Error)

            'TODO: Email the exception and setup the current state
            Dim mailer As New NotifyMail()
            mailer.SendServiceMail("VOEIS Hub Push Service - Error", "SQL Exception in the DataMonitor class." & Environment.NewLine & sqlex.Message.ToString)
            Throw sqlex
        Catch ex As Exception
            'TODO: make this a little more robust - I need to actually let the user know why this didn't work 
            System.Diagnostics.EventLog.WriteEntry("VOEIS Hub Push Service", "SQL Exception in the DataMonitor class." & Environment.NewLine & ex.Message.ToString, EventLogEntryType.Error)

            'TODO: Email the exception and setup the current state
            Dim mailer As New NotifyMail()
            mailer.SendServiceMail("VOEIS Hub Push Service - Error", "SQL Exception in the DataMonitor class." & Environment.NewLine & ex.Message.ToString)

            Throw ex
        End Try

        Return arylstTables
    End Function

    Public Shared Function SetConnectionString()
        Dim strConn As String = ""
        Try
            strConn = "Data Source=" & My.Settings.ServiceSQLServer & ";Initial Catalog=" & My.Settings.ServiceDatabase & ";"
            If My.Settings.ServiceAuthentication = "Windows" Then
                strConn &= "Integrated Security=SSPI;"
            Else
                strConn &= "User Id=" & My.Settings.ServiceUserName & ";Password=" & My.Settings.ServicePassword & ";"
            End If

        Catch ex As Exception
            System.Diagnostics.EventLog.WriteEntry("VOEIS Hub Push Service", "Error connecting to SQL Server: " & Environment.NewLine & ex.Message.ToString & Environment.NewLine & "Service will now be terminated.", EventLogEntryType.Error)
            Throw ex
        End Try

        Return strConn

    End Function

End Class
